<?php
namespace zhtfrontend\controllers;

use common\components\helpers\Util;
use common\models\datas\Type;
use yii\db\Query;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\components\helpers\Funs;
use zhtfrontend\components\services\goods\AuditAction;
use zhtfrontend\components\services\goods\ReportAction;
use zhtfrontend\components\services\goods\SetDistributionAction;
use zhtfrontend\components\services\goods\SetListAction;
use zhtfrontend\components\services\goods\ShelveAction;
use zhtfrontend\components\services\goods\UpdateAction;
use zhtfrontend\components\services\goods\UpdateSetAction;
use zhtfrontend\models\datas\Arrangement;
use zhtfrontend\models\datas\ArtRelation;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\Course;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\GoodsGroup;
use yii\web\BadRequestHttpException;
use yii\helpers\Json;
use yii\web\Response;
use zhtfrontend\models\datas\Goods;
use common\models\datas\Cate;
use yii\data\Pagination;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\GoodsAttr;
use yii\widgets\ActiveForm;
use common\models\datas\CateAttr;
use common\components\helpers\Tree;
use common\models\datas\TypeBrand;
use common\models\datas\Brand;
use yii\helpers\ArrayHelper;
use zhtfrontend\models\datas\Mechanic;
use yii\web\NotFoundHttpException;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\models\datas\Tutor;

class MechanicgoodsController extends BaseController{

    public function actions(){
        return [
            'setlist'=>[
                'class'=>SetListAction::className(),
            ],
            'updateprice'=>[
                'class'=>UpdateSetAction::className(),
            ],
            'shelve'=>[
                'class'=>ShelveAction::className(),
            ],
            'set'=>[
                'class'=>SetDistributionAction::className(),
            ],
            'report'=>[
                'class'=>ReportAction::className()
            ],
            'audit'=>[
                'class'=>AuditAction::className()
            ]
        ];
    }

    public function actionIndex()
    {
           $tree=$this->getCategory(1);
           $search_data=\Yii::$app->request->get();
            $tree = str_replace('&nbsp;',' ' ,$tree);
            $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
            $mechanic_id = isset(\Yii::$app->user->identity->mechanic_id)?\Yii::$app->user->identity->mechanic_id:0;
            $goods=Goods::find()->where(['type_id'=>1,'is_delete'=>0,'teaching_methods'=>1,'mechanic_id'=>$mechanic_id]);
            if (!empty(\Yii::$app->request->get('goods_name'))) {
                $goods->andWhere(['like', 'name', \Yii::$app->request->get('goods_name')]);
            }
            if (!empty($search_data['cate_name']) ){
                $cateids[]=$search_data['cate_name'];
                $cate=Cate::findOne($search_data['cate_name']);
                $cates = [];
                if($cate->parent_id == 0){
                    $cates = Cate::find()->select('id')->where(['parent_id'=>$search_data['cate_name']])->column();
                }
                $cateids = array_merge($cates,$cateids);
                $cate = Cate::find()->select('id')->Where(['in', 'id', $cateids])->column();
                $goods->andWhere(['in', 'cat_id', $cate]);
            }
            if(!empty($search_data['id'])){
                $goods->andWhere(['id'=>$search_data['id']]);
            }

            $pages = new Pagination(['totalCount' => $goods->count(), 'pageSize' => $pageSize,'params'=>array_merge($_GET,$search_data)]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $goods->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model, ['cate_name','class_time','status_name','mechanicoperate']);
        return $this->render('index',['tree'=>$tree,'data'=>$array,'pages'=>$pages]);
    }
    public function getCategory($type_id){
        $site_id=Fun::getSiteId();
        $cate_attr= CateAttr::find()->select(['cat_id'])->where(['or',['mobile_is_show'=>1],['pc_is_show'=>1]])->andWhere(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
        $category=Cate::find()->indexBy('id')->where(['in','id',$cate_attr])->andWhere(['type_id'=>$type_id,'is_delete'=>0])->asArray()->all();
        //$category = Cate::find()->indexBy('id')->where(['is_delete'=>0,'type_id'=>$type_id])->asArray()->all();
        $tree = (new Tree($category))->get_tree_select();
        return  $tree;
    }

}